// pages/addaddress/addaddress.js
const util = require("../../utils/util")
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:{},
    array: ['请选择','我的地址', '客服地址'],
    type:['','my_address','client_address'],
    i: 0,
    province:0,
    district:0,
    city:0,
    name:"",
    tel:"",
    detail:""
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      i: e.detail.value
    })
  },
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      [e.currentTarget.dataset.id]: e.detail.value
    })
  },
  bindInput(e){
    this.setData({
      [e.currentTarget.dataset.id]:e.detail.value
    })
  },
  bindSave(){
    let that = this
    wx.showLoading({
      title: '加载中'
    })
    console.log({
      "addressType": that.data.type[that.data.i],
      "cityCode": that.data.list[that.data.province].childs[that.data.city] ? that.data.list[that.data.province].childs[that.data.city].key : 0,
      "consignee": that.data.name,
      "detailAddress": that.data.detail,
      "districtCode": that.data.list[that.data.province].childs[that.data.city].childs[that.data.district] ? that.data.list[that.data.province].childs[that.data.city].childs[that.data.district].key : 0,
      "mobile": that.data.tel,
      "provinceCode": that.data.list[that.data.province].key
    })
    util.post({
      url:"https://groomtea.icebartech.com/api/miniApp/dealer/saveShippingAddress",
      success:(res)=>{
        console.log(res) 
        app.titleToast(res.msg)
        if(res.status=='200'){
          wx.navigateBack({ delta: 1 })
        }
        
      },
      data:{
        "addressType": that.data.type[that.data.i],
        "cityCode": that.data.list[that.data.province].childs[that.data.city] ? that.data.list[that.data.province].childs[that.data.city].key : 0,
        "consignee": that.data.name,
        "detailAddress": that.data.detail,
        "districtCode": that.data.list[that.data.province].childs[that.data.city].childs[that.data.district] ? that.data.list[that.data.province].childs[that.data.city].childs[that.data.district].key : 0,
        "mobile": that.data.tel,
        "provinceCode": that.data.list[that.data.province].key
      },
      fail:(error)=>{
        console.log(error)
        wx.hideLoading()
        app.titleToast(error.msg)
      },
      complete:function(){
          wx.hideLoading()
          
      }
    })
  } ,
  onLoad:function(options){
    let that = this
    wx.request({
      url: "https://groomtea.icebartech.com/api/base/sys/mini/loadDictionary?codeType=province",
      header: { "Accept": "application/json;charset=UTF-8" },
      method: 'post',
      success(res) {
        console.log(res)
        that.setData({list:res.data.data.bussData})
      }
    })
  }
})